//
//  CS_Page_SignatureButtonView.m
//  CSUIKit
//
//  Created by 浙大网新中研软件 on 2018/10/26.
//

#import "CS_Page_SignatureButtonView.h"

@interface CS_Page_SignatureButtonView()

@property (nonatomic, strong) CS_Page_Button *page_Button;

@property (nonatomic, strong) NSString *buttonName;

@property (nonatomic, copy) SignatureClickHandlerBlock signatureClickHandler;

@property (nonatomic, strong) UIImageView *signatureImageView;

@end

@implementation CS_Page_SignatureButtonView

+(instancetype)signatureButtonView:(NSString *)buttonName signatureImage:(UIImage *)signatureImage signatureClickHandler:(SignatureClickHandlerBlock)signatureClickHandler{
    
    CS_Page_SignatureButtonView *signatureButtonView = [CS_Page_SignatureButtonView new];
    signatureButtonView.signatureClickHandler = signatureClickHandler;
    signatureButtonView.buttonName = buttonName;
    signatureButtonView.signatureImage = signatureImage;
    
    return signatureButtonView;
    
}

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    CGFloat localPrimaryY = 0.0;
    CGSize superViewSize = superView.frame.size;
    
    //如果图片存在的话
    if (self.signatureImage) {
        
        self.signatureImageView.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, 0, superViewSize.width - 2 * CELL_EDIT_RIGHT_MARGIN, (superViewSize.width - 2 * CELL_EDIT_RIGHT_MARGIN)/2.0);
        self.signatureImageView.image = self.signatureImage;
        [self addSubview:self.signatureImageView];
        localPrimaryY += (superViewSize.width - 2 * CELL_EDIT_RIGHT_MARGIN)/2.0;
        
    }
    
    if (self.isLightStyle) {
        self.page_Button.isOtherStyle = YES;
    }
    localPrimaryY = [self.page_Button layoutView:self primaryY:localPrimaryY];
    
    self.frame = CGRectMake(0, primaryY, superViewSize.width, localPrimaryY);
    
    [superView addSubview:self];
    
    return primaryY + localPrimaryY;
    
}

-(void)setSignatureImage:(UIImage *)signatureImage{
    
    _signatureImage = signatureImage;
    
    self.isRefreshRequired = YES;
    
    [self refreshSelfAndAdjustContext];
    
    
}

-(UIImageView *)signatureImageView{
    
    if (_signatureImageView == nil) {
        _signatureImageView = [[UIImageView alloc] init];
    }
    return _signatureImageView;
    
}

-(CS_Page_Button *)page_Button{
    
    if (_page_Button == nil) {
        _page_Button = [CS_Page_Button new];
        _page_Button.buttonName = _buttonName;
        WS(weakSelf);
        _page_Button.clickHandler = ^{
            weakSelf.signatureClickHandler(weakSelf);
        };
    }
    return _page_Button;
    
}

@end
